package com.uobgroup.ews.uam.core.domain;

import com.uobgroup.ews.uam.core.dao.UAMUserDao;
import com.uobgroup.ews.uam.core.service.UAMUserService;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class UAMUserDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UAMUser> data;

	@Autowired
    UAMUserService uAMUserService;

	@Autowired
    UAMUserDao uAMUserDao;

	public UAMUser getNewTransientUAMUser(int index) {
        UAMUser obj = new UAMUser();
        setIsActive(obj, index);
        setProxyUser(obj, index);
        setUsername(obj, index);
        return obj;
    }

	public void setIsActive(UAMUser obj, int index) {
        Boolean isActive = Boolean.TRUE;
        obj.setIsActive(isActive);
    }

	public void setProxyUser(UAMUser obj, int index) {
        String proxyUser = "proxyUser_" + index;
        if (proxyUser.length() > 32) {
            proxyUser = proxyUser.substring(0, 32);
        }
        obj.setProxyUser(proxyUser);
    }

	public void setUsername(UAMUser obj, int index) {
        String username = "username_" + index;
        if (username.length() > 32) {
            username = new Random().nextInt(10) + username.substring(1, 32);
        }
        obj.setUsername(username);
    }

	public UAMUser getSpecificUAMUser(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UAMUser obj = data.get(index);
        Long id = obj.getId();
        return uAMUserService.findUAMUser(id);
    }

	public UAMUser getRandomUAMUser() {
        init();
        UAMUser obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return uAMUserService.findUAMUser(id);
    }

	public boolean modifyUAMUser(UAMUser obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = uAMUserService.findUAMUserEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UAMUser' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UAMUser>();
        for (int i = 0; i < 10; i++) {
            UAMUser obj = getNewTransientUAMUser(i);
            try {
                uAMUserService.saveUAMUser(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            uAMUserDao.flush();
            data.add(obj);
        }
    }
}
